package com.nf.health.app.datamain.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ImageView;

import com.nf.health.app.R;
import com.nf.health.app.activity.global.BaseActivity;
import com.nf.health.app.adapter.OnMyClickListener;
import com.nf.health.app.adapter.OxygenHistoryDataAdapter;
import com.nf.health.app.customview.DragListView;
import com.nf.health.app.models.BloodoHistoryOygenBean;
import com.nf.health.app.models.OxyDeviceDataBean;
import com.nf.health.app.utils.ViewUtil;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 血氧页面
 * Created by Administrator on 2016/5/24.
 */
public class BloodOxygenHistoryActivity extends BaseActivity {
    private final String OXYGENTION_HISTORY = "com.nf.health.app.activity";
    private final String OXYGENTION_HISTORY_DEL_BY_ID = "com.nf.health.app.activity.del.byId";
    @Bind(R.id.titlebar_back_view)
    ImageView titlebar_back_view;
    @Bind(R.id.history_oxygen_listview)
    DragListView history_oxygen_listview;
    private int pageNo = 1;
    private String pagesize = "20";
    private OxygenHistoryDataAdapter adapter;
    private ArrayList<OxyDeviceDataBean> list;
    private int del_index = 0;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitleBar(R.layout.titlebar_base);
        setContent(R.layout.activity_oxygen_history);
        ButterKnife.bind(this);
        ViewUtil.setText(this, R.id.titlebar_title_tv, "历史记录");
        initListener();
        initView();
        pageNo = 1;
        initData(pageNo);
    }
    private void initListener(){
        titlebar_back_view.setVisibility(View.VISIBLE);
        titlebar_back_view.setOnClickListener(this);
    }
    private void initView() {
        // TODO Auto-generated method stub
        history_oxygen_listview.setRefreshableAndLoadMoreable(true, true);
        history_oxygen_listview.setOnRefreshAndLoadMoreListener(new DragListView.onRefreshAndLoadMoreListener() {

            @Override
            public void onRefresh() {
                pageNo = 1;
                initData(pageNo);
            }

            @Override
            public void onLoadMore() {
                pageNo++;
                initData(pageNo);
            }
        });
        list = new ArrayList<>();
        adapter = new OxygenHistoryDataAdapter(BloodOxygenHistoryActivity.this,list);
        history_oxygen_listview.setAdapter(adapter);
        history_oxygen_listview.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                adapter.closeAllOpenedSwipeView();
                adapter.setOxygenHistoryDataRefresh(list);
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

            }
        });
    }
    @Override
    public void handleActionSuccess(String actionName, Object object) {
        super.handleActionSuccess(actionName, object);
        if (OXYGENTION_HISTORY.equals(actionName)) {
            BloodoHistoryOygenBean bloodoHistoryOygenBean = (BloodoHistoryOygenBean) object;
            ArrayList<OxyDeviceDataBean> getlist = bloodoHistoryOygenBean.getDeviceData();
            if(pageNo == 1){
                list.clear();
            }
            if(getlist != null){
                list.addAll(getlist);
                if(adapter == null){
                    adapter = new OxygenHistoryDataAdapter(BloodOxygenHistoryActivity.this,list);
                    history_oxygen_listview.setAdapter(adapter);
                }else{
                    adapter.setOxygenHistoryDataRefresh(list);
                }
            }
            adapter.setOnMyClickListener(new OnMyClickListener() {
                @Override
                public void onClick(int position, String flag) {
                    provider.delOxygenHistoryById("oxygen",list.get(position).getId()+"",OXYGENTION_HISTORY_DEL_BY_ID);
                    del_index = position;
                    adapter.closeAllOpenedSwipeView();
                    adapter.setOxygenHistoryDataRefresh(list);
                }
            });
            history_oxygen_listview.completeRefresh();
            history_oxygen_listview.completeLoadMore();
        }if (OXYGENTION_HISTORY_DEL_BY_ID.equals(actionName)) {
            list.remove(del_index);
            adapter.setOxygenHistoryDataRefresh(list);
        }
    }
    // onrestart 进入此页面刷新方法
    @SuppressWarnings("unchecked")
    protected void onResume() {
        super.onResume();
    }

    private void initData(int pageNo) {
        provider.getOxygenHistory(pageNo + "", pagesize, OXYGENTION_HISTORY);
    }

        @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            //右边按钮的点击事件
            case R.id.titlebar_back_view:
                finish();
            default:
                break;
        }
    }
}
